<template>
<div>
    <Form ref="form" :model="params">
        <Row :gutter="24" type="flex">
            <Col>
            <RadioGroup v-model="daysradio" type="button" @on-change="handleChangeVisitType(daysradio)">
                <Radio label="2">
                    昨日
                </Radio>
                <Radio label="5">
                    周
                </Radio>
            </RadioGroup>
            </Col>
            <Col>
            <FormItem prop="start_time">
                <DatePicker transfer type="daterange" @on-change="Submits" :options="options" v-model="timer"
                    placeholder="请选择过期日期" />
            </FormItem>
            </Col>
            <Col span="5">
            <FormItem prop="keywords">
                <Input @on-enter="askDatas" @on-clear="askData" v-model="params.keywords" clearable
                    placeholder="请输入患者姓名/手机号/诊疗号" />
            </FormItem>
            </Col>
            <Col>
            <FormItem prop="belong_assistant_id">
                <Select v-if="+edit_type===1" transfer v-model="belong_id" @on-change="askData"
                    placeholder="所属客服" :clearable="true">
                    <Option v-for="item in baseInfo.member_customer_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
                <Select v-else-if="+edit_type===2" transfer v-model="belong_id" @on-change="askData"
                    placeholder="所属医助" :clearable="true">
                    <Option v-for="item in baseInfo.member_medical_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem prop="outpatient_type_id">
                <Select transfer v-model="params.outpatient_type_id" @on-change="askData"
                    placeholder="就诊类型" :clearable="true">
                    <Option v-for="item in baseInfo.outpatient_type_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem prop="clinic_assess_id">
                <Select transfer v-model="params.clinic_id" placeholder="请选择诊室" :clearable="true"
                    @on-change="askData">
                    <Option v-for="item in baseInfo.clinic_list" :key="item.id" :value="item.id" :disabled="+item.status === 0">
                        {{ item.name }}</Option>
                </Select>
            </FormItem>
            </Col>
            <Col>
            <FormItem prop="patient_diseases_id">
                <div style="position: relative;">
                    <Select transfer v-model="params.fault_diagnosis_type" placeholder="请选择断诊次数" :clearable="true"
                        @on-change="askData">
                        <Option :value="1">1次</Option>
                        <Option :value="2">2次</Option>
                        <Option :value="3">3次</Option>
                        <Option :value="4">3次以上</Option>
                    </Select>
                </div>
            </FormItem>
            </Col>
            <Col>
                <FormItem>
                    <Select transfer v-width="140" v-model="params.in_hospital" placeholder="请选择住院状态" :clearable="true"
                        @on-change="askData">
                        <Option v-for="item in baseInfo.hospital" :key="item.value" :value="item.value" >{{item.label}}</Option>
                    </Select>
                </FormItem>
            </Col>
        </Row>
    </Form>
    <tableList
        :edit_type="edit_type"
        :params="params"
        :loading="loading"
        :data="data"
        :total="total"
        @askData="askData"
        @sortchange="sortchange" 
    />
</div>
</template>
<script>
    import { GetUnarriveList } from '@/api/patient'
    import { getFirstDayOfWeek, getFirstDayOnWeek, getFirstMonthOnWeek } from '@/utils/datetime';
    import tableList from './table-list';
    export default {
        components: { tableList },
        props: ['edit_type'],
        data() {
            return {
                timer: [],
                options: {
                    disabledDate(date) {
                        return date && date.valueOf() > Date.now() - 86400000;
                    }
                },
                belong_id: '',
                params:  {
                    start_time: '',
                    end_time: '',
                    keywords: '',
                    outpatient_type_id: '',
                    effect_type_id: '',
                    risk_type_id: '',
                    clinic_assess_id: '',
                    body_parts_ids: [],
                    clinic_id: '',
                    tags_ids: [],
                    sort_last_revisiting_type: 0,
                    sort_next_revisiting_type: 0,
                    sort_last_callback_type: 0,
                    sort_next_callback_type: 0,
                    sort_revisiting_type: 0,
                    sort_callback_type: 0,
                    sort_missed_type: 0,
                    sort_patient_card_id: 0,
                    page: 1,
                    pageSize: 10,
                    patient_diseases_id: '',
                    fault_diagnosis_type: '',
                    in_hospital:'',
                },
                loading: false,
                baseInfo: {
                    body_parts_list: [],
                    tags_list: [],
                    member_list: [],
                    effect_type_list: [],
                    risk_type_list: [],
                    clinic_assess_list: [],
                    outpatient_type_list: [],
                    patient_diseases_list: [],
                    member_medical_list: [],
                    hospital:[]
                },
                grid: {
                    xl: 4,
                    lg: 8,
                    md: 12,
                    sm: 24,
                    xs: 24
                },
                data: [],
                total: 0,
                daysradio: '1',
                nowDayTime: '',
                nowWeekStart: '',
                nowWeekEnd: '',
                nowMonthStart: '',
                nowMonthEnd: '',
                yesterdayT: '',
                // baseInfo: {
                //     body_parts_list: [],
                //     tags_list: [],
                //     member_list: [],
                //     effect_type_list: [],
                //     risk_type_list: [],
                //     clinic_assess_list: [],
                //     outpatient_type_list: [],
                //     patient_diseases_list: [],
                //     hospital:[]
                // },
            }
        },
        mounted() {
            let jumpPage = window.localStorage.getItem('jumpDetail')
            if (jumpPage) {
                let parsePage = JSON.parse(jumpPage)
                this.params.belong_assistant_id = parsePage.belong.toString()
            }
            this.initial()
        },
        methods: {
            initial() {
                this.getNowDay()
            },
            askData() {
                this.loading = true
                let params = JSON.parse(JSON.stringify(this.params))
                params.edit_type = this.edit_type
                params.end_time = this.timer[1]?this.$Date(this.timer[1]).format('YYYY-MM-DD'):''
                params.start_time = this.timer[0]?this.$Date(this.timer[0]).format('YYYY-MM-DD'):''
                params.body_parts_ids = this.params.body_parts_ids.join(',')
                params.tags_ids = this.params.tags_ids.join(',')
                if (+this.edit_type === 1) {
                    params.belong_member_id = this.belong_id
                } else if (+this.edit_type === 2) {
                    params.belong_assistant_id = this.belong_id
                }
                GetUnarriveList(params).then(res => {
                    this.total = res.total
                    this.data = res.list
                    this.$emit('alltotal', {num: 4, total: res.total})
                    for (let i = 0; i < this.data.length; i++) {
                        this.$set(this.data[i], 'show', false)
                    }
                    if (this.baseInfo.body_parts_list.length === 0 &&
                        this.baseInfo.clinic_assess_list.length === 0 &&
                        this.baseInfo.effect_type_list.length === 0 &&
                        this.baseInfo.member_list.length === 0 &&
                        this.baseInfo.outpatient_type_list.length === 0 &&
                        this.baseInfo.risk_type_list.length === 0 &&
                        this.baseInfo.tags_list.length === 0) {
                        this.baseInfo = res.condition
                    }
                }).catch(() => {
                    this.loading = false
                }).finally(() => {
                    this.loading = false
                })
            },
            askDatas() {
                this.daysradio = ''
                this.timer = []
                this.params.start_time = ''
                this.params.end_time = ''
                this.belong_id = ''
                this.params.outpatient_type_id = ''
                this.params.effect_type_id = ''
                this.params.risk_type_id = ''
                this.params.clinic_assess_id = ''
                this.params.body_parts_ids = []
                this.params.clinic_id = ''
                this.params.tags_ids = []
                this.params.sort_last_revisiting_type = 0
                this.params.sort_next_revisiting_type = 0
                this.params.sort_last_callback_type = 0
                this.params.sort_next_callback_type = 0
                this.params.sort_revisiting_type = 0
                this.params.sort_callback_type = 0
                this.params.sort_missed_type = 0
                this.params.page = 1
                this.params.pageSize = 10
                this.params.patient_diseases_id = ''
                this.params.fault_diagnosis_type = ''
                this.params.in_hospital=''
                this.askData()
            },
            sortchange(e) {
                this.params.sort_last_revisiting_type = 0
                this.params.sort_next_revisiting_type = 0
                this.params.sort_last_callback_type = 0
                this.params.sort_next_callback_type = 0
                this.params.sort_revisiting_type = 0
                this.params.sort_callback_type = 0
                this.params.sort_missed_type = 0
                this.params.sort_patient_card_id = 0
                switch (e.key) {
                    case 'patient_card_id':
                        this.params.sort_patient_card_id = e.sort
                        break;
                    case 'last_revisiting_time_str':
                        this.params.sort_last_revisiting_type = e.sort
                        break;
                    case 'next_revisiting_time_str':
                        this.params.sort_next_revisiting_type = e.sort
                        break;
                    case 'last_callback_time_str':
                        this.params.sort_last_callback_type = e.sort
                        break;
                    case 'next_callback_time_str':
                        this.params.sort_next_callback_type = e.sort
                        break;
                    case 'revisiting_count':
                        this.params.sort_revisiting_type = e.sort
                        break;
                    case 'missed_days':
                        this.params.sort_missed_type = e.sort
                        break;
                    case 'callback_count':
                        this.params.sort_callback_type = e.sort
                        break;
                }
                this.askData()
            },
            // 切换日期
            handleChangeVisitType(e) {
                switch (e) {
                    case '1':
                        const nowYears = this.$Date(this.nowDayTime).format('YYYY-MM-DD')
                        this.timer = [this.nowDayTime, this.nowDayTime]
                        break
                    case '2':
                        const nowYearStart = this.$Date(this.yesterdayT).format('YYYY-MM-DD')
                        this.timer = [nowYearStart, nowYearStart]
                        break
                    case '3':
                        const yesterday = this.$Date(this.yesterday).format('YYYY-MM-DD')
                        this.timer = [this.nowDayTime, yesterday]
                        break
                    case '4':
                        const yesterdays = this.$Date(this.yesterdays).format('YYYY-MM-DD')
                        this.timer = [yesterdays, yesterdays]
                        break
                    case '5':
                        this.timer = [this.yestweek, this.yesterdayT]
                        break
                    case '6':
                        this.timer = []
                        break
                    default:
                        this.timer = [this.nowDayTime, this.nowDayTime]
                        break
                }
                this.askData()
            },
            
            Submits(e) {
                const startTime = e[0] || ''
                const endTime = e[1] || ''
                const nowDay = this.$Date(this.nowDayTime).format('YYYY-MM-DD') // 当天
                const nowMonthStart = this.$Date(this.yesterdayT).format('YYYY-MM-DD')
                const nowMonthEnd = this.$Date(this.yesterdays).format('YYYY-MM-DD')
                const nowWeekStart = this.$Date(this.yesterday).format('YYYY-MM-DD')
                const nowWeekEnd = this.$Date(this.yestweek).format('YYYY-MM-DD')
                if (startTime === nowMonthStart && endTime === nowMonthStart) {
                    this.daysradio = '2'
                    this.handleChangeVisitType(this.daysradio)
                } else if (startTime === nowWeekEnd && endTime === nowMonthStart) {
                    this.daysradio = '5'
                    this.handleChangeVisitType(this.daysradio)
                } else {
                    this.daysradio = ''
                    this.askData()
                }
            },
            getNowDay() {
                const now = new Date()
                const nowMonth = now.getMonth()
                const nowYear = now.getFullYear()
                this.nowDayTime = this.$Date(now).format('YYYY-MM-DD')
                const time = (new Date()).getTime() - (24 * 60 * 60 * 1000)
                const times = (new Date()).getTime() + (7 * 24 * 60 * 60 * 1000)
                const etime = (new Date()).getTime() + (24 * 60 * 60 * 1000)
                const week = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000)
                this.yesterdayT = new Date(time)
                this.yesterday = new Date(times)
                this.yesterdays = new Date(etime)
                this.yestweek = new Date(week)
                this.nowMonthStart = new Date(nowYear, nowMonth, 1)
                this.nowMonthEnd = getFirstMonthOnWeek(new Date())
                this.nowWeekStart = getFirstDayOfWeek(new Date())
                this.nowWeekEnd = getFirstDayOnWeek(new Date())
                const nowYears = this.$Date(this.nowDayTime).format('YYYY-MM-DD')
                this.nowDayTime = nowYears
                this.start_time = [this.nowDayTime, this.nowDayTime]
                this.handleChangeVisitType(this.daysradio)
            },
        },
    }
</script>
